package com.dpteam.dao.impl;

import com.dpteam.dao.RejectOrderDao;
import com.dpteam.model.RejectOrder;
import com.dpteam.util.Page;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.List;

/**
 * Created by xwh on 2016/1/8.
 */
@Repository
public class RejectOrderDaoImpl extends BaseDaoImpl<RejectOrder> implements RejectOrderDao {

    private static DecimalFormat df=new java.text.DecimalFormat("#.##");

    @Override
    public List<RejectOrder> findAllRejectOrder() {
        String hql = "from RejectOrder order by id ";
        return find(hql);
    }

    @Override
    public List<RejectOrder> findAllRejectOrderPage(Page page) {
        String hql = "from RejectOrder order by id ";
        return find(hql, page);
    }

    @Override
    public List<RejectOrder> findByUser(int userId) {
        String hql = "from RejectOrder where userId = ? order by id ";
        return find(hql, userId);
    }

    @Override
    public List<RejectOrder> findByUserPage(int userId, Page page) {
        String hql = "from RejectOrder where userId = ? order by id ";
        return find(hql, page, userId);
    }

    @Override
    public List<RejectOrder> findByProduct(int productId) {
        String hql = "from RejectOrder where productId = ? order by id ";
        return find(hql, productId);
    }

    @Override
    public List<RejectOrder> findByProductPage(int productId, Page page) {
        String hql = "from RejectOrder where productId = ? order by id ";
        return find(hql, page, productId);
    }

    @Override
    public List<RejectOrder> findByTime(LocalDate timeBegin, LocalDate timeEnd) {
        LocalDateTime beginDateTime = LocalDateTime.of(timeBegin, LocalTime.now());
        LocalDateTime endDateTime = LocalDateTime.of(timeEnd, LocalTime.now());
        String hql = "from RejectOrder where time >= ? and time <= ? order by id ";
        return find(hql, beginDateTime, endDateTime);
    }

    @Override
    public List<RejectOrder> findByTimePage(LocalDate timeBegin, LocalDate timeEnd, Page page) {
        LocalDateTime beginDateTime = LocalDateTime.of(timeBegin, LocalTime.now());
        LocalDateTime endDateTime = LocalDateTime.of(timeEnd, LocalTime.now());
        String hql = "from RejectOrder where time >= ? and time <= ? order by id ";
        return find(hql, page, beginDateTime, endDateTime);
    }

    @Override
    public List<RejectOrder> findByUserIdAndTime(int userId, LocalDate timeBegin, LocalDate timeEnd) {
        LocalDateTime beginDateTime = LocalDateTime.of(timeBegin, LocalTime.now());
        LocalDateTime endDateTime = LocalDateTime.of(timeEnd, LocalTime.now());
        String hql = "from RejectOrder where userId = ? and time >= ? and time <= ? order by id ";
        return find(hql, userId, beginDateTime, endDateTime);
    }

    @Override
    public List<RejectOrder> findByUserIdAndTimePage(int userId, LocalDate timeBegin, LocalDate timeEnd, Page page) {
        LocalDateTime beginDateTime = LocalDateTime.of(timeBegin, LocalTime.now());
        LocalDateTime endDateTime = LocalDateTime.of(timeEnd, LocalTime.now());
        String hql = "from RejectOrder where userId = ? and time >= ? and time <= ? order by id ";
        return find(hql, page, userId, beginDateTime, endDateTime);
    }

    @Override
    public void deleteById(int id) {
        RejectOrder rejectOrder = get(RejectOrder.class, id);
        if (rejectOrder != null) {
            delete(rejectOrder);
        }
    }

    @Override
    public double sumProfit(int year) {
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, 1, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, 12, 31), LocalTime.MAX);
        String hql = "select sum(profitTotal) from RejectOrder where time >= ? and time <= ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }

    @Override
    public double sumProfit(int year, int month) {
        Month month1 = Month.of(month);
        int maxDays = month1.maxLength();
        LocalDate localDate = LocalDate.of(year, month, 1);
        if (!localDate.isLeapYear() && month == 2) {
            maxDays--;
        }
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, month, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, month, maxDays), LocalTime.MAX);
        String hql = "select sum(profitTotal) from RejectOrder where time >= ? and time <= ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }

    @Override
    public double sumProfit(int year, int month, int categoryId) {
        Month month1 = Month.of(month);
        LocalDate localDate = LocalDate.of(year, month, 1);
        int maxDay = month1.maxLength();
        if (!localDate.isLeapYear() && month == 2) {
            maxDay--;
        }
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, month, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, month, maxDay), LocalTime.MAX);
        String hql = "select sum(profitTotal) from RejectOrder where time >= ? and time <= ? and product.category.id = ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        query.setParameter(2, categoryId);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }

    @Override
    public double sumSell(int year) {
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, 1, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, 12, 31), LocalTime.MAX);
        String hql = "select sum(sellPriceTotal) from RejectOrder where time >= ? and time <= ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }

    @Override
    public double sumSell(int year, int month) {
        Month month1 = Month.of(month);
        int maxDays = month1.maxLength();
        LocalDate localDate = LocalDate.of(year, month, 1);
        if (!localDate.isLeapYear() && month == 2) {
            maxDays--;
        }
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, month, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, month, maxDays), LocalTime.MAX);
        String hql = "select sum(sellPriceTotal) from RejectOrder where time >= ? and time <= ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }

    @Override
    public double sumSell(int year, int month, int categoryId) {
        Month month1 = Month.of(month);
        LocalDate localDate = LocalDate.of(year, month, 1);
        int maxDay = month1.maxLength();
        if (!localDate.isLeapYear() && month == 2) {
            maxDay--;
        }
        LocalDateTime begin = LocalDateTime.of(LocalDate.of(year, month, 1), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.of(year, month, maxDay), LocalTime.MAX);
        String hql = "select sum(sellPriceTotal) from RejectOrder where time >= ? and time <= ?  and product.category.id = ?";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter(0, begin);
        query.setParameter(1, end);
        query.setParameter(2, categoryId);
        Double result = (Double)query.uniqueResult();
//        return result == null ? 0 : result.doubleValue();
        return result == null ? 0 : Double.parseDouble(df.format(result));
    }
}
